/* $Id: MediaManager.java 39 2009-07-24 22:48:06Z h_m_r@gmx.de $ */
package util;

import java.awt.Image;
import java.util.HashMap;

import javax.swing.ImageIcon;

public class MediaManager {

	private static HashMap<Class<?>, HashMap<String, Object>> media = new HashMap<Class<?>, HashMap<String, Object>>();

	private static HashMap<String, Object> getClassMap(Class<?> clazz) {
		if(!media.containsKey(clazz))
			media.put(clazz, new HashMap<String, Object>());
		return media.get(clazz);
	}

	private static Object getFromMap(Class<?> clazz, String path) {
		HashMap<String, Object> map = getClassMap(Image.class);
		return map.containsKey(path) ? map.get(path) : null;
	}

	private static void addToMap(Class<?> clazz, String path, Object value) {
		HashMap<String, Object> map = getClassMap(Image.class);
		if(value != null)
			map.put(path, value);
	}

	public static Image getImage(String path) {
		Object result = getFromMap(Image.class, path);
		if(result == null) {
			result = GUIUtils.createImage(path);
			addToMap(Image.class, path, result);
		}
		return (Image)result;
	}

	public static ImageIcon getImageIcon(String path) {
		Object result = getFromMap(ImageIcon.class, path);
		if(result == null) {
			result = GUIUtils.createImageIcon(path);
			addToMap(ImageIcon.class, path, result);
		}
		return (ImageIcon)result;
	}

}
